package com.example.demo1.Controller;

import cn.yueshutong.springbootstartercurrentlimiting.annotation.CurrentLimiter;
import com.example.demo1.Bean.UserBean;
import com.example.demo1.Bean.UserStates;
import com.example.demo1.Service.UserService;
import com.example.demo1.Service.UserstatusService;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.chrono.ChronoLocalDate;
import java.util.Date;
import java.util.List;

@Controller

public class UserLoginCon {
    @Resource
    UserService userservice;
    @Resource
    UserstatusService userstaservice;

    UserBean userBean=null;
    UserStates userstates=null;
    List<UserStates> userstates1=null;
    String tem1=null;
    String sta=null;
    String count=null;

    @RequestMapping("/select")
    public String SelectuserLogin(@AuthenticationPrincipal User user,Model model) throws JsonProcessingException {
        System.out.println("进入select");
        if(user==null)
            return "error";
        LocalDate nyr = LocalDate.now();
        if(user.getAuthorities().toString().contains("ROLE_USER")){
            System.out.println(user.getUsername());
            userBean = userservice.getUserByname(user.getUsername());
            int x = userBean.getUserId();
            count = String.valueOf(userBean.isLogin());
            model.addAttribute("count", count);
            System.out.println(userBean);
            if(!userBean.getLastdate().isEqual(nyr)) {
                System.out.println("未打卡数据");
                model.addAttribute("userBean", userBean);
                model.addAttribute("nyr", nyr);
                System.out.println(userBean);
                return "submit";
            }
            else {
                System.out.println("已打卡数据");
                userstates1 = userstaservice.SearchUserStatusByid(String.valueOf(x));
                System.out.println(userstates1);
                if(userstates1.size()!=0) {

                    System.out.println("1111111111");
                    tem1 = userstates1.get(0).getTem();
                    sta = userstates1.get(0).getStatus();
                    model.addAttribute("nyr", nyr);
                    model.addAttribute("tem", tem1);
                    model.addAttribute("status", sta);
                    model.addAttribute("userBean", userBean);
                }
                else{
                    System.out.println("222222222222");
                    model.addAttribute("nyr", nyr);
                    model.addAttribute("userBean", userBean);
                    return "submit";
                }


                return "submit";
            }
        }
        else
            return "redirect:/admin/user";
    }
    @RequestMapping("/usersubmit")
    public String UserSubmit(Model model,String username,String UserId,String status,String tem){
        LocalDate nyr = LocalDate.now();
        if(username==null)
            return "error";
        userstaservice.updateUserStatus(username,UserId,status,nyr,tem);
        userBean = userservice.getUserByname(username);
        userservice.updateUserLastDate(UserId,nyr);

        model.addAttribute("userBean",userBean);
        model.addAttribute("nyr",nyr);
        model.addAttribute("tem",tem);
        model.addAttribute("status",status);
        model.addAttribute("count", String.valueOf(userBean.isLogin()));
        return "submit";
    }
}
